package com.app.fragment;

import android.app.Activity;
import android.app.ListFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.app.activity.MainActivity;
import com.app.adapter.GroupArrayAdapter;
import com.app.constants.ItemMenu;
import com.app.constants.Messages;
import com.app.constants.Resources;
import com.app.model.ListGroup;
import com.app.service.HttpRequest;
import com.app.service.HttpRequestListener;
import com.app.tourismmer.R;
import com.app.util.Util;
import com.app.util.UtilGson;

public class HomeFragment extends ListFragment implements HttpRequestListener {

    GroupArrayAdapter groupAdapter;
    ListView listView;
    Button btnLoadMore;
    int firstResult = 0;
    final int amount = 5;

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

	View rootView = inflater.inflate(R.layout.fragment_home, container, false);
	listView = (ListView) rootView.findViewById(R.id.listview);

	HttpRequest httpRequest = new HttpRequest(HttpRequest.GET, this);
	String link = Resources.GROUP + Util.appendLink("getTopGroups", amount, firstResult);
	httpRequest.execute(link);

	groupAdapter = new GroupArrayAdapter(rootView.getContext());
	setListAdapter(groupAdapter);
	
	return rootView;
    }
    
    public void loadData() {
//	listView.removeAllViews();
	HttpRequest httpRequest = new HttpRequest(HttpRequest.GET, this);
	String link = Resources.GROUP + Util.appendLink("getTopGroups", amount, firstResult);
	httpRequest.execute(link);
	firstResult = firstResult + amount;
    }

    public void addButtonMoreData() {
	// Creating a button - Load More
	btnLoadMore = new Button(getActivity());
	btnLoadMore.setId(123456);
	btnLoadMore.setWidth(getListView().getWidth());
	btnLoadMore.setTextColor(Color.WHITE);
	btnLoadMore.setBackgroundColor(Color.parseColor("#222617"));
	btnLoadMore.setText("Mais Dados");
	
	btnLoadMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
        	loadData();
            }
	});
	
	
	// Adding button to listview at footer
	listView.addFooterView(btnLoadMore);
    }

    @Override
    public void onAttach(Activity activity) {
	super.onAttach(activity);
	((MainActivity) activity).onSectionAttached(ItemMenu.HOME);
    }

    @Override
    public void onRequestSucess(String response) {

	ListGroup listGroup = UtilGson.jsonToListGroup(response);

	if (Messages.SUCCESS.getStatusCode().equals(listGroup.getStatusCode())) {
	    
	    if(firstResult==0 && listGroup.getHasMoreData()) {
		groupAdapter.setData(listGroup.getListGroup());
		addButtonMoreData();
		
	    } else if(listGroup.getHasMoreData()) {
		groupAdapter.addList(listGroup.getListGroup());
		
	    } else {
		boolean sera = listView.removeFooterView(btnLoadMore);
		System.out.println(sera);
	    }

	} else {
	    Toast.makeText(this.getActivity(), "Ops, ocorreu um erro!", Toast.LENGTH_LONG).show();
	}

    }

    @Override
    public void onRequestFailed() {
	Toast.makeText(this.getActivity(), "Ops, ocorreu um erro!", Toast.LENGTH_LONG).show();
    }
    
    public void onClickMoreData(View view) {
	Toast.makeText(this.getActivity(), "Mais Dados", Toast.LENGTH_LONG).show();
    }

}
